package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjNetworkStrategy;
import com.zb.mapper.ZjNetworkStrategyMapper;
import com.zb.service.ZjNetworkStrategyService;
import com.zb.service.ZjNetworkWithdrawWithFeeEmpRootService;
import com.zb.vo.NetworkStrategyVo;
import com.zb.vo.NetworkWithdrawWithFeeEmpRootUpdateVo;
import com.zb.vo.OrderWithdrawHandlingFeeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点策略表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Service
public class ZjNetworkStrategyServiceImpl extends ServiceImpl<ZjNetworkStrategyMapper, ZjNetworkStrategy> implements ZjNetworkStrategyService {
    @Resource
    private ZjNetworkStrategyMapper networkStrategyMapper;
    @Resource
    private ZjNetworkWithdrawWithFeeEmpRootService networkWithdrawWithFeeEmpRootService;

    // 保存或更改服务商策略
    @Override
    public void save(Long custAccId, Long id, NetworkStrategyVo strategy) {
        ZjNetworkStrategy newStrategy = new ZjNetworkStrategy();
        BeanUtil.copyProperties(strategy, newStrategy);
        if (null != this.getById(id)) {
            newStrategy.setId(id);
            this.updateById(newStrategy);
        } else {
            newStrategy.setId(id);
            newStrategy.setCustAccId(custAccId);
            this.save(newStrategy);
        }
        networkWithdrawWithFeeEmpRootService.save(custAccId, id, strategy.getWithdrawEmpRootVos(), strategy.getSelectFeeEmpRootVos());
    }

    // 根据ID获取接收策略
    @Override
    public ZjNetworkStrategy isAutoSureReceiveOrder(Long id) {
        ZjNetworkStrategy strategy = this.getById(id);
        // 默认自动接单
        if (null == strategy) {
            strategy = new ZjNetworkStrategy();
            strategy.setAutoSureReceiveOrder(true);
            strategy.setAutoCheckEndOrder(true);
        }
        return strategy;
    }

    // 批量更新服务商接单策略
    @Override
    public void updateAutoSureReceiveOrderBatchById(List<Long> ids, boolean autoSureReceiveOrder) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkStrategyMapper.updateAutoSureReceiveOrderBatchById(ids, autoSureReceiveOrder);
    }

    @Override
    public void updateAutoCheckCancelOrderBatchById(List<Long> ids, boolean autoCheckCancelOrder) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkStrategyMapper.updateAutoCheckCancelOrderBatchById(ids, autoCheckCancelOrder);
    }

    // 批量更新服务商审核结单策略
    @Override
    public void updateAutoCheckEndOrderBatchById(List<Long> ids, boolean autoCheckEndOrder) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkStrategyMapper.updateAutoCheckEndOrderBatchById(ids, autoCheckEndOrder);
    }

    // 批量更新允许更改备件价格
    @Override
    public void updateEnableUpdateSparePriceBatchById(List<Long> ids, boolean enableUpdateSparePrice) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkStrategyMapper.updateEnableUpdateSparePriceBatchById(ids, enableUpdateSparePrice);
    }

    // 批量更改提现手续费
    @Override
    public void updateOrderWithdrawHandlingFee(OrderWithdrawHandlingFeeVo handlingFeeVo) {
        if (CollectionUtil.isEmpty(handlingFeeVo.getNetworkIds())) {
            return;
        }
        List<ZjNetworkStrategy> strategyList = new ArrayList<>();
        ZjNetworkStrategy networkStrategy = null;
        for (Long id : handlingFeeVo.getNetworkIds()) {
            networkStrategy = new ZjNetworkStrategy();
            BeanUtil.copyProperties(handlingFeeVo, networkStrategy);
            networkStrategy.setId(id);
            strategyList.add(networkStrategy);
        }
        if (CollectionUtil.isNotEmpty(strategyList)) this.updateBatchById(strategyList);
    }

    // 获取手续费策略
    @Override
    public Map<String, Object> getNetworkWithdrawHandlingFeeStrategy(Long networkId) {
        ZjNetworkStrategy strategy = this.getById(networkId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("withdrawHandlingFeeStrategy", strategy.getWithdrawHandlingFeeStrategy());
        resultMap.put("handingFeeProportion", strategy.getHandingFeeProportion());
        resultMap.put("handingFeeFixedOrder", strategy.getHandingFeeFixedOrder());
        return resultMap;
    }

    // 批量更改提现策略
    @Override
    public void updateWithdrawEmpRoot(Long custAccId, NetworkWithdrawWithFeeEmpRootUpdateVo withdrawEmpRootVo) {
        List<ZjNetworkStrategy> networkStrategyList = new ArrayList<>();
        ZjNetworkStrategy strategy = null;
        for (Long id : withdrawEmpRootVo.getNetworkIds()) {
            strategy = new ZjNetworkStrategy();
            strategy.setId(id);
            strategy.setAllowWithdrawal(withdrawEmpRootVo.getAllow());
            strategy.setAllowManagerHelpWithdraw(withdrawEmpRootVo.getAllowManagerHelpWithdraw());
            networkStrategyList.add(strategy);
        }
        this.updateBatchById(networkStrategyList);
        networkWithdrawWithFeeEmpRootService.save(custAccId, 1, withdrawEmpRootVo);
    }

    // 批量更改查询费用策略
    @Override
    public void updateSelectFeeEmpRoot(Long custAccId, NetworkWithdrawWithFeeEmpRootUpdateVo withdrawEmpRootVo) {
        List<ZjNetworkStrategy> networkStrategyList = new ArrayList<>();
        ZjNetworkStrategy strategy = null;
        for (Long id : withdrawEmpRootVo.getNetworkIds()) {
            strategy = new ZjNetworkStrategy();
            strategy.setId(id);
            strategy.setAllowSelectFee(withdrawEmpRootVo.getAllow());
            networkStrategyList.add(strategy);
        }
        this.updateBatchById(networkStrategyList);
        networkWithdrawWithFeeEmpRootService.save(custAccId, 2, withdrawEmpRootVo);
    }

}
